/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panelAdmin;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Chinh
 */
public class ListItem {
    
    public ListItem() {
    }
    
    public ArrayList<Item> LoadListItem() {
        ArrayList<Item> listItem = new ArrayList<Item>();
        File f = new File("item.txt");
        try {
            FileReader fr = new FileReader(f);
            LineNumberReader lnr = new LineNumberReader(fr);
            String item = "";
            while (true) {
                item = lnr.readLine();
                if (item == null) {
                    break;
                }
                if (item.contains("#")) {
                    continue;
                }
                String id = "";
                String name = "";
                String type = "";
                String stPrice = "";
                long price = 0;
                String link = "";
                StringTokenizer tokens = new StringTokenizer(item, "|");
                for (int i = 0; i < 5; i++) {
                    if (i == 0) {
                        id = tokens.nextToken();
                    }
                    if (i == 1) {
                        name = tokens.nextToken();
                    }
                    if (i == 2) {
                        type = tokens.nextToken();
                    }
                    if (i == 3) {
                        stPrice = tokens.nextToken();
                        price = Integer.parseInt(stPrice);
                    }
                    if (i == 4) {
                        link = tokens.nextToken();
                    }
                }
                listItem.add(new Item(id, name, type, price, link));
            }
            lnr.close();
            fr.close();
        } catch (FileNotFoundException ex) {
        } catch (IOException ex) {
        }
        return listItem;
    }
    
    public void SaveListItem(ArrayList<Item> listItem) {
        File f = new File("item.txt");
        try {
            FileWriter fw = new FileWriter(f);
            PrintWriter pw = new PrintWriter(fw);
            pw.println("#ID|Name|Type|Price|Link");
            for (int i = 0; i < listItem.size(); i++) {
                Item item = listItem.get(i);
                pw.println(item.getIdItem() + "|" + item.getNameItem() + "|" + item.getTypeItem() + "|" + item.getPrice() + "|" + item.getLinkItem());
            }
            pw.close();
            fw.close();
        } catch (IOException ex) {
            Logger.getLogger(ListCustommer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
//
//class App1 {
//    
//    public static void main(String[] args) {
//        ListItem list = new ListItem();
//        ArrayList<Item> listItem = list.LoadListItem();
//        System.out.println(listItem.get(1).toString());
//        listItem.get(0).setPrice(2000);
//        list.SaveListItem(listItem);
//    }
//}